/**
*  \file control_iso_valve.c
*
*  \brief
*
*  \author Michael J. Hohmann  <michael.hohmann@linde-mh.de>
*
*
*/

/*===============================================[ private includes  ]================================================*/
#include "control_iso_valve.h"
#include "../assertor/assertor.h"
#include "../pal/pal_mem.h"
#include "../interpol/interpol_u16_u16.h"
#include "../contactor/contactor.h"

/*===============================================[ private defines   ]================================================*/

/*===============================================[ private datatypes ]================================================*/

/** Private variables */
struct control_iso_valve_private
{
	struct contactor_obj iso_valve;
};

/*===============================================[ private variables ]================================================*/
static struct control_iso_valve_private control_iso_valve;

static const U16 tighten_x[]={U16_MIN,	19,  	20, 	60, 	61, 	U16_MAX };
static const U16 tighten_y[]={0,       	0,     	0xA000, 0x4000, 0, 		0       };

static const U16 hold_x[]	={U16_MIN, 	19, 	20,     60,	    61, 	U16_MAX };
static const U16 hold_y[]	={0,       	0,    	0xFFFF, 0x2000, 0, 		0       };

static const struct interpol_u16_u16_obj pwm_tighten= { 6, tighten_x, tighten_y };
static const struct interpol_u16_u16_obj pwm_hold   = { 6, hold_x,    hold_y    };

/*===============================================[ private functions ]================================================*/

/*================================================[ inline functions ]================================================*/

/*================================================[ public functions ]================================================*/
RC control_iso_valve_initialize( void )
{
	BO rv=RC_ERROR;

	pal_memzero( &control_iso_valve, sizeof control_iso_valve);

   rv=contactor_construct( &control_iso_valve.iso_valve,  	&pwm_tighten, &pwm_hold, 1000 );

   return rv;
}


U16 control_iso_valve_10ms( BO main_contactor , enum trilean driver_present, F32 supply_voltage )
{
	BO valve_on = (driver_present==trilean_true)  &&  (main_contactor) ;

	return contactor_service( &control_iso_valve.iso_valve, supply_voltage, valve_on );
}


/*===============================================[ private functions ]================================================*/


/***********************************************************************************************************************
*                             Copyright 2020 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/
